package cn.flying.admin.controller.process;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.flying.base.common.constants.Constants;
import cn.flying.base.core.common.utils.Ft;
import cn.flying.base.process.domain.cf.CfProcConfig;
import cn.flying.base.process.domain.cf.CfTaskPosition;
import cn.flying.base.process.service.ProcessDefinitionService;

/**
 * @description: 流程管理
 * @author: lvyong
 * @date: 2023年05月23日 22:04
 * @version: 1.0
 */
@Controller
@RequestMapping("process")
public class ProcessViewController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private ProcessDefinitionService processDefinitionService;

    /**
     * 流程定义列表页面
     *
     * @return
     */
    @RequestMapping("deployment/list")
    public String deploymentList(Model model) {
        return "process/deployment/list";
    }

    /**
     * 流程业务配置列表页面
     *
     * @return
     */
    @RequestMapping("bizConfig/list")
    public String bizConfigList(Model model) {
        String tenantId = (String) Ft.ThreadLocal.get(Constants.TENANT_ID);
        Map<String, String> defKeys = processDefinitionService.getProcDefKeys(tenantId);
        model.addAttribute("defKeys", defKeys);
        return "process/bizConfig/list";
    }

    /**
     * 流程业务配置新增/修改页面
     *
     * @return
     */
    @RequestMapping("bizConfig/edit")
    public String bizConfigEdit(Long id, Model model) {
        String tenantId = (String) Ft.ThreadLocal.get(Constants.TENANT_ID);
        Map<String, String> bizKeys = new HashMap<>();
        bizKeys.put("CREDIT", "授信申请");
        model.addAttribute("bizKeys", bizKeys);
        Map<String, String> defKeys = processDefinitionService.getProcDefKeys(tenantId);
        model.addAttribute("defKeys", defKeys);
        CfProcConfig config;
        if (id == null) {
            config = new CfProcConfig();
        } else {
            config = processDefinitionService.findProcBizConfigById(id);
        }
        model.addAttribute("config", config);
        return "process/bizConfig/edit";
    }

    /**
     * 流程权限列表页面
     *
     * @return
     */
    @RequestMapping("identity/list")
    public String identityList(Model model) {
        return "process/identity/list";
    }

    /**
     * 流程任务节点权限配置列表页面
     *
     * @return
     */
    @RequestMapping("identity/task/list")
    public String taskIdentityList(String processDefId, Model model) {
        model.addAttribute("processDefId", processDefId);
        return "process/identity/task-identity-list";
    }

    /**
     * 流程任务节点权限配置新增/修改页面
     *
     * @return
     */
    @RequestMapping("task-identity/turn/edit")
    public String turnEdit(String processDefId, Long id, Model model) {
        model.addAttribute("processDefId", processDefId);
        Map<String, String> userTasks = processDefinitionService.getAllUserTask(processDefId);
        model.addAttribute("userTasks", userTasks);
        CfTaskPosition taskPosition = new CfTaskPosition();
        List<String> positions = new ArrayList<>();
        if (Objects.nonNull(id)) {
            taskPosition = processDefinitionService.findProcTaskPositionById(id);
            positions = Stream.of(taskPosition.getPostId().split(",")).collect(Collectors.toList());
        }
        model.addAttribute("positions", positions);
        model.addAttribute("taskPosition", taskPosition);
        return "process/identity/task-identity-edit";
    }

    /**
     * 流程实例查询列表页面
     *
     * @return
     */
    @RequestMapping("instance/list")
    public String instanceList(Model model) {
        String tenantId = (String) Ft.ThreadLocal.get(Constants.TENANT_ID);
        Map<String, String> defKeys = processDefinitionService.getProcDefKeys(tenantId);
        model.addAttribute("defKeys", defKeys);
        return "process/instance/list";
    }
}
